<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" id="message">
    <button id="submit">提交</button>

    <script>
        // 创建 websocket实例
        let webSocket = new WebSocket("ws://127.0.0.1:8080/test");
        // 挂载回调函数
        webSocket.onopen = function() {
            console.log("连接建立");
        }
        webSocket.onerror = function() {
            console.log("连接异常");
        }
        webSocket.onclose = function() {
            console.log("连接关闭");
        }
        webSocket.onmessage = function(e) {
            console.log("接收到消息：" + e.data);
        }

        // 实现点击提交后，通过 webSocket 发送请求
        let input = document.querySelector('#message');
        let button = document.querySelector('#submit');
        button.onclick = function() {
            console.log("发送消息：" + input.value);
            webSocket.send(input.value);
        }
    </script>
</body>
</html>